//以下为APP的配置
window.CONF = {
	 //APP效果图的宽度
	'width' : 640,
	 //加载插件列表
	'plus' : [
		'plus_fixedwidth.js',
		'plus_touch.js',
		'plus_btntouch.js'
	]
};

~function(){
	
	window.Q = {
		"$$" : function(id){
			return document.getElementById(id);
		},
		"PC" : function(){
			return (window.navigator.platform=="Win32");
		},
		"loadJS" : function(url){
			//同步加载JS文件
			var xmlHttp = new XMLHttpRequest();
			xmlHttp.open("GET",url,false);
			xmlHttp.send(null);
			var _head = document.getElementsByTagName("head").item(0);
			var _script = document.createElement( "script" );
			_script.language = "javascript";
			_script.type = "text/javascript";
			_script.text = xmlHttp.responseText;
			_head.appendChild( _script );
		},
		"isArray" : function(v){
			//是否数组
			return toString.apply(v) === '[object Array]';
		},	
		"UID" : function(name){
			var _counter = (localStorage['_counter']||1)*1;
			var charCode = name.charCodeAt(name.length - 1);
			if (charCode >= 48 && charCode <= 57) name += "_";
			localStorage['_counter'] = _counter + 1;
			return name + localStorage['_counter'];
		},
		"ready" : function(callback){
			//封装appcan加载完毕事件
			Q.readyCall = Q.readyCall || [];
			Q.readyCall.push(callback);
		},
		"resize" : function(callback){
			//封装appcan加载完毕事件
			Q.resizeCall = Q.resizeCall || [];
			Q.resizeCall.push(callback);
		},
		"fn" : function(json){
			//加载插件方法
			for(var k in json){
				if(k in Q){
					alert('插件方法 '+ k + ' 已存在，请更换其他方法名');
					return;
				}
				Q[k] = json[k];	
			}
		}
	};
	
	//加载插件和css
	Q.loadJS('js/airportJS/zepto.min.js');
	Q.$ = Zepto; // <- 定义私有$，防止和JQ冲突
	for(var i=0;i<CONF.plus.length;i++)	Q.loadJS('js/airportJS/'+ CONF.plus[i]);		
	//将所有的iframe用uex打开
	function _fixIframe(){
		var iframe = Q.$('.iframe');
		
		iframe.each(function(){
			var me = Q.$(this),
			src = me.data('src'),
			ID = me.attr('id');
			if(!ID){
				ID = Q.UID('window');
				me.attr('id',ID);
			}
			var bound = Q.$(me.parent())[0].getBoundingClientRect();
			
			me.data('bound',JSON.stringify(bound));
			
			if(!Q.isAppCan){
				//修复IOS web中iframe无法拖动的问题
				if (navigator.userAgent.match(/iPad|iPhone/i)){
					Q.$(me.parent()).removeClass('fixIOSIframe').addClass('fixIOSIframe');
				}
				me.attr('id',ID+'_FRAME');
				//me.attr('src',src).css({"width":bound.width,"height":bound.height});
				//alert(bound.width+":"+Q.sizeZoom(bound.width));
				Q.$(me.parent()).prepend('<iframe id="'+ ID +'" src="'+ src +'" style="width:'+ bound.width +'px;height:'+ bound.height +'px"></iframe>');
				$('#'+ID).data('bound',JSON.stringify(bound));
			}else{
				Q.uexWindow.openPopover(ID,"0",src,"",bound.left,bound.top,bound.width,bound.height,"","0");
			}
		});	
	}
	
	//uexonload触发封装
	var uexOnload = function(){
		
		var hashKey = '_onceByHash'+ location.hash;
		if(localStorage[hashKey]){
			eval(localStorage[hashKey]);
			localStorage.removeItem(hashKey);
		}
		_fixIframe();
		//页面加载完毕时
		if(Q.isArray(Q.readyCall)){
			for(var i=0,l=Q.readyCall.length;i<l;i++){
				Q.readyCall[i]();	
			}
			delete Q.readyCall;
		}
	};
	
	window.uexOnload = function(type){
		Q.isAppCan = true;
		if (!type && !window._uexOnload_){
			window._uexOnload_ = true;
			uexOnload();	
		}
	};
	
	Q.$(function(){
		setTimeout(function(){
			if(!Q.isAppCan){
				Q.isAppCan = false;
				window.scroll(0,1);
				uexOnload();
			}
		},200);
	});
	
	//处理设置转屏事件
	window.onorientationchange=window.onresize=function(){
		if(Q._resizeTimer)clearTimeout(Q._resizeTimer);
		Q._resizeTimer = setTimeout(function(){
			if(Q.isArray(Q.resizeCall)){
				for(var i=0,l=Q.resizeCall.length;i<l;i++){
					Q.resizeCall[i]();	
				}
			}
		},150);		
	}
	
	//覆盖windowopen事件
	Q.window = {
		'open' : function(url,windowName,flag,inAniID,animDuration){
			if(!Q.isAppCan){
				parent.location.href=url;
			}else{
				if(!windowName || windowName=='')windowName=url;
				uexWindow.open(windowName,0,url,inAniID||0,'','',flag||0,animDuration||0);
			}
		},
		'close' : function(winid){
			if(!Q.isAppCan){
				parent.window.history.go(-1);
			}else{
				if(!winid){
					uexWindow.close();
				}else{
					Q.call(function(){
						uexWindow.close();
					},'',winid);	
				}
			}
		},
		'onceByHash' : function(hash,js){
			localStorage['_onceByHash'+ hash] = js;	
		},
		//以下方法为iframe的封装
		'iframe' : function(url,windowName){
			uexWindow.evaluatePopoverScript('',windowName,"location.href='"+ url+"'");
		},
		'movePopWin':function(winName,x,callback){
			uexWindow.evaluatePopoverScript("",winName,"Q.window._moveThisWin('"+ x +"','"+ (callback||'') +"')");
			return this;
		},
		'_moveThisWin':function(x,callback){
			uexWindow.beginAnimition();
			uexWindow.setAnimitionDelay('0');
			uexWindow.setAnimitionDuration('250');
			uexWindow.setAnimitionAutoReverse('0');
			uexWindow.makeTranslation(x,'0','0');
			uexWindow.makeScale('1','1','1');
			uexWindow.commitAnimition();
			uexWindow.bringToFront();
			callback && callback();
		}
	};
	
	//根据设备类型，确定用click还是toustart
	window.tap = "click";
	window.touchstart = "mousedown";
	window.touchmove = "mousemove";
	window.touchend = "mouseup";
	
	if('ontouchstart' in window){
		window.tap='tap';
		window.touchstart = "touchstart";
		window.touchmove = "touchmove";
		window.touchend = "touchend";
	}
	
	//跨窗口调用封装
	Q.call = function(fun,parm,winID,popID){
		if(typeof(fun)=='string'){
			var f = fun;
		}else{
			var f = '(' + fun.toString() + ')(' + JSON.stringify(parm||'') + ')';	
		}
		if(winID=='' && popID==''){
			alert('callFun时winID或popID必须有一个值存在');	
			return;
		}
		if(popID && popID!=''){
			if(!Q.isAppCan){
				Q.$$(popID).contentDocument.location.href = "javascript:try{"+ f +"}catch(e){}";
			}else{
				uexWindow.evaluatePopoverScript(winID||'',popID,f);
			}
			return;
		}
		if(winID!=''){
			if(!Q.isAppCan){
				parent.location.href = "javascript:try{"+ f +"}catch(e){}";
			}else{
				uexWindow.evaluateScript(winID,0,f);
			}
			return;
		}
	};
	
	//IFRAME组处理，仅在打包后可使用
	function IframeGroup(id){
		this._groupID = id;
		this._currIframe = null;
		return this;
	}
	IframeGroup.prototype.bound = function(){
		var bound = Q.$(this._groupID).data('bound');
		if(!bound){
			bound =  Q.$(this._groupID)[0].getBoundingClientRect();	
			Q.$(this._groupID).data('bound',JSON.stringify(bound))
		}
		return bound;
	};
	IframeGroup.prototype.open = function(src,ID,ani){
		var bound = this.bound();
		Q.uexWindow.openPopover(ID,"0", (src+'#'+ID) ,"",bound.left + bound.width*ani - 1 ,bound.top,bound.width,bound.height,"","0");
		if(ani!=0){
			//Q.window.movePopWin('"+ this._currIframe +"','"+ Q.posZoom(bound.left + bound.width*ani*-1) +"');
			Q.window.onceByHash('#'+ID, "Q.window._moveThisWin('"+ Q.posZoom(bound.left + bound.width*ani*-1) +"')" );
		}
		this._currIframe = ID;
		return this;
	};
	Q.iframeGroup = function(id){
		if(!Q.window._iframeGroupData || !Q.window._iframeGroupData[id]){
			Q.window._iframeGroupData = Q.window._iframeGroupData || [];	
			Q.window._iframeGroupData[id] = new IframeGroup(id);			
		}
		return 	Q.window._iframeGroupData[id];
	};
	
}();


